using System.Collections.Generic;
using I2.Loc;
using UnityEngine;

public class MatchRewardManager : MonoBehaviour
{
	[SerializeField]
	public List<InfoMatchRewardForGO> listLoadingFromGD = new List<InfoMatchRewardForGO>();

	public Dictionary<TypeGame, Dictionary<int, List<InfoMatchReward>>> dictionaryListReward = new Dictionary<TypeGame, Dictionary<int, List<InfoMatchReward>>>();

	private bool initialized;

	public void Init()
	{
		if (!initialized)
		{
			initialized = true;
			ParseListToDictionary("TDM");
			ParseListToDictionary("TTDM");
			ParseListToDictionary("RACE");
			ParseListToDictionary("INFEC");
			ParseListToDictionary("ROCKET");
			ParseListToDictionary("SKYBLOCK");
			ParseListToDictionary("COPSANDROB");
			ParseListToDictionary("PIXEL");
			ParseListToDictionary("WARROB");
			ParseListToDictionary("TITANFALL");
		}
	}

	private void Start()
	{
	}

	public static string GetName(InfoMatchReward reward)
	{
		if (reward.id == "gold")
		{
			return ScriptLocalization.Get("Marathon/Coins");
		}
		if (reward.id == "cash")
		{
			return ScriptLocalization.Get("Marathon/Cash");
		}
		if (reward.id == "star")
		{
			return ScriptLocalization.Get("Game_Common/vip_acc_experience");
		}
		Product product = MarketController.Instance.GetProduct(reward.id);
		if (product != null)
		{
			return ScriptLocalization.Get("Market/" + product.id + "_title");
		}
		return null;
	}

	public static Texture GetTexture(InfoMatchReward reward)
	{
		if (reward.id == "cash")
		{
			return ReputationProductRewardWindow.instance.textureCash;
		}
		if (reward.id == "gold")
		{
			return ReputationProductRewardWindow.instance.textureGold;
		}
		if (reward.id == "star")
		{
			return ReputationProductRewardWindow.instance.textureStar;
		}
		return MarketController.Instance.GetProductTexture(reward.id);
	}

	public void ParseListToDictionary(string nameTypeGame)
	{
		int num = 1;
		int num2 = 1;
		List<InfoMatchReward> list = new List<InfoMatchReward>();
		Dictionary<int, List<InfoMatchReward>> dictionary = new Dictionary<int, List<InfoMatchReward>>();
		for (int i = 0; i < listLoadingFromGD.Count; i++)
		{
			if (!(listLoadingFromGD[i].mode != nameTypeGame))
			{
				num2 = listLoadingFromGD[i].place;
				if (num2 != num)
				{
					dictionary.Add(num, list);
					list = new List<InfoMatchReward>();
					num = num2;
					list.Clear();
				}
				InfoMatchReward item = default(InfoMatchReward);
				item.count = listLoadingFromGD[i].count;
				item.id = listLoadingFromGD[i].id;
				item.weight = listLoadingFromGD[i].weight;
				list.Add(item);
			}
		}
		dictionary.Add(num2, list);
		switch (nameTypeGame)
		{
		case "TDM":
			dictionaryListReward.Add(TypeGame.TDM, dictionary);
			break;
		case "TTDM":
			dictionaryListReward.Add(TypeGame.tanks, dictionary);
			break;
		case "RACE":
			dictionaryListReward.Add(TypeGame.race, dictionary);
			break;
		case "INFEC":
			dictionaryListReward.Add(TypeGame.Zombe, dictionary);
			break;
		case "ROCKET":
			dictionaryListReward.Add(TypeGame.RocketLeague, dictionary);
			break;
		case "SKYBLOCK":
			dictionaryListReward.Add(TypeGame.SkyBlock, dictionary);
			break;
		case "COPSANDROB":
			dictionaryListReward.Add(TypeGame.CopsAndRobbers, dictionary);
			break;
		case "PIXEL":
			dictionaryListReward.Add(TypeGame.Pixelmon, dictionary);
			break;
		case "WARROB":
			dictionaryListReward.Add(TypeGame.WalkingRobots, dictionary);
			break;
		case "TITANFALL":
			dictionaryListReward.Add(TypeGame.TitanfallV2, dictionary);
			break;
		}
	}

	public InfoMatchReward GetReward(TypeGame type, int place, int playersCount = 0, int bonusFactor = 1, bool isActiveVip = false, TDMResult result = TDMResult.None)
	{
		Init();
		InfoMatchReward result2 = default(InfoMatchReward);
		result2.id = string.Empty;
		Dictionary<int, List<InfoMatchReward>> value = new Dictionary<int, List<InfoMatchReward>>();
		bool flag = dictionaryListReward.TryGetValue(type, out value);
		if (!flag)
		{
			return result2;
		}
		List<InfoMatchReward> value2 = new List<InfoMatchReward>();
		switch (type)
		{
		case TypeGame.Zombe:
			if (ZombieModePlayerStatistics.team == PunTeams.Team.blue)
			{
				flag = value.TryGetValue(1, out value2);
			}
			else if (ZombieModePlayerStatistics.team == PunTeams.Team.red)
			{
				flag = value.TryGetValue(2, out value2);
			}
			break;
		case TypeGame.RocketLeague:
			flag = value.TryGetValue(1, out value2);
			break;
		case TypeGame.CopsAndRobbers:
			flag = value.TryGetValue(GetPlaceCopsRobs(), out value2);
			break;
		default:
			flag = value.TryGetValue(GetPlace(place, playersCount), out value2);
			break;
		}
		if (!flag)
		{
			return result2;
		}
		int num = 0;
		foreach (InfoMatchReward item in value2)
		{
			if ((result.Equals(TDMResult.Loser) || result.Equals(TDMResult.Draw)) && item.id.Equals("cash"))
			{
				num += item.weight;
			}
			else if (result.Equals(TDMResult.Winner))
			{
				num += item.weight;
			}
		}
		if (num == 0)
		{
			return result2;
		}
		int num2 = Random.Range(1, num);
		int num3 = 0;
		foreach (InfoMatchReward item2 in value2)
		{
			if ((result.Equals(TDMResult.Loser) || result.Equals(TDMResult.Draw)) && !item2.id.Equals("cash"))
			{
				continue;
			}
			num3 += item2.weight;
			if (num3 < num2)
			{
				continue;
			}
			if (result.Equals(TDMResult.Loser) || result.Equals(TDMResult.Draw))
			{
				InfoMatchReward result3 = item2;
				float num4 = 1f;
				if (result.Equals(TDMResult.Loser))
				{
					num4 = 0.3f;
				}
				else if (result.Equals(TDMResult.Draw))
				{
					num4 = 0.5f;
				}
				result3.count = (int)((float)result3.count * num4);
				return result3;
			}
			if (result.Equals(TDMResult.Winner))
			{
				return item2;
			}
		}
		return result2;
	}

	public int GetPlace(int myPlace, int playersCount)
	{
		int num = 7;
		int result = 0;
		if (PhotonNetwork.room == null)
		{
			return result;
		}
		List<PhotonPlayer> list = PhotonNetwork.player.SortedTeam();
		switch (GameController.instance.curTypeGame)
		{
		case TypeGame.race:
			num = PhotonNetwork.room.maxPlayers;
			return num - RaceController.instance._playerCountInRace + myPlace;
		case TypeGame.tanks:
			num = PhotonNetwork.room.maxPlayers / 2;
			return num - playersCount + myPlace;
		case TypeGame.TDM:
			num = PhotonNetwork.room.maxPlayers / 2;
			return num - playersCount + myPlace;
		case TypeGame.SkyBlock:
			num = PhotonNetwork.room.maxPlayers / 2;
			return num - playersCount + myPlace;
		default:
			num = PhotonNetwork.room.maxPlayers / 2;
			return num - playersCount + myPlace;
		}
	}

	private int GetPlaceCopsRobs()
	{
		if (TDMController.myTeam == PunTeams.Team.red)
		{
			return (CopsAndRobbersController.instance.gates.Count - CopsAndRobbersController.instance.gatesDestroyed <= 0) ? 1 : (CopsAndRobbersController.instance.gates.Count - CopsAndRobbersController.instance.gatesDestroyed);
		}
		if (TDMController.myTeam == PunTeams.Team.blue)
		{
			return 3;
		}
		return 1;
	}
}
